package com.itheima.freemarker.controller;

import com.itheima.freemarker.pojo.Student;
import freemarker.template.TemplateException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

//不要使用@RestController
@Controller// return "01-bastic"会从templates下找模版然后渲染数据并返回浏览器
public class HelloController {


    @GetMapping("basic")
    public String basic(Model model){
        Student student = new Student();
        student.setAge(18);
        student.setBirthday(new Date());
        student.setName("张三");
        student.setMoney(100F);

        model.addAttribute("stu", student);
        model.addAttribute("name", null);

        return "01-bastic";//templates下面的模版名字
    }


    @GetMapping("list")
    public String list(Model model){
        //封装list存放所有学生
        List<Student> students = new ArrayList<>();
        Student student = new Student();
        student.setName("小明");
        student.setAge(18);
        student.setMoney(20.0f);
        students.add(student);

        Student student2 = new Student();
        student2.setName("小红");
        student2.setAge(18);
        student2.setMoney(30.0f);
        students.add(student2);

        model.addAttribute("stus", students);
        return "02-list";
        //请求转发 classpath:/templates/02-list.ftl
    }



    @GetMapping("map")
    public String map(Model model){

        //封装list存放所有学生
        Map students = new HashMap<>();

        Student student = new Student();
        student.setName("小明");
        student.setAge(18);
        student.setMoney(20.0f);
        students.put("stu1",student);

        Student student2 = new Student();
        student2.setName("小红");
        student2.setAge(18);
        student2.setMoney(30.0f);
        students.put("stu2",student2);
        /**
         * {
         *     "stuMap":{
         *         "stu1":{name:小明,age:18}
         *         "stu2":{name:小红,age:18}
         *     }
         * }
         */
        model.addAttribute("stuMap",students);
        return "03-map";
        //请求转发 classpath:/templates/03-map.ftl

    }



    @GetMapping("if")
    public String iftest(Model model){
        //封装list存放所有学生
        List<Student> students = new ArrayList<>();
        Student student = new Student();
        student.setName("小明");
        student.setAge(18);
        student.setMoney(20.0f);
        students.add(student);

        Student student2 = new Student();
        student2.setName("小红");
        student2.setAge(18);
        student2.setMoney(30.0f);
        students.add(student2);

        Student student3 = new Student();
        student3.setName("小蓝");
        student3.setAge(18);
        student3.setMoney(30.0f);
        students.add(student3);

        model.addAttribute("stus", students);
        return "04-if";
        //请求转发 classpath:/templates/04-if.ftl
    }



    @GetMapping("operator")
    public String operator(Model model) throws InterruptedException {
        model.addAttribute("date1",new Date());
        Thread.sleep(1000);
        model.addAttribute("date2",new Date());
        return "05-operator";
    }



    @GetMapping("testnull")
    public String testnull(Model model) {

        //封装list存放所有学生
        List<Student> list = new ArrayList<>();
        Student student = new Student();
        student.setName("小明");
        student.setAge(18);
        student.setMoney(20.0f);
        list.add(student);

        Student student2 = new Student();
        student2.setName(null);
        student2.setAge(18);
        student2.setMoney(30.0f);
        list.add(student2);

        model.addAttribute("stus", list);

        return "06-testnull";
    }



    @GetMapping("fun")
    public String fun(Model model) {
        //1.1 小强对象模型数据
        Student stu1 = new Student();
        stu1.setName("小强");
        stu1.setAge(18);
        stu1.setMoney(1000.86f);
        stu1.setBirthday(new Date());
        //1.2 小红对象模型数据
        Student stu2 = new Student();
        stu2.setName("小红");
        stu2.setMoney(200.1f);
        stu2.setAge(19);
        //1.3 将两个对象模型数据存放到List集合中
        List<Student> stus = new ArrayList<>();
        stus.add(stu1);
        stus.add(stu2);
        model.addAttribute("stus", stus);
        // 2.1 添加日期
        Date date = new Date();
        model.addAttribute("today", date);
        // 3.1 添加数值
        model.addAttribute("point", 102920122);
        return "07-fun";
    }

}
